package com.jbh.recordlibrary.u;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

import com.jbh.recordlibrary.f.cheap.CheapWAV;
import com.jbh.recordlibrary.i.CallBack;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class FileUtil {



    /**
     * 指定区间  剪切文件
     *
     * @param mFile     源文件名字
     * @param start     开始帧数
     * @param end       结束帧数
     */

    public static void cutWAVRecordFile(final Context context,
                                        final File mFile,
                                        final int start ,
                                        final int end,
                                        final CallBack<String> callBack){
        try {
            CheapWAV a=new CheapWAV();
            /**加载wav文件*/
            a.ReadFile(mFile);

            /**获取音频文件总帧数*/
            int numFrames = a.getNumFrames();
            LogUtil.LOG_V("[WriteFile]->numFrames = "+numFrames);


            String path = SdcardUtil.getSaveDir(context)+File.separator+"cut_files";
            File cutFile=new File(path);
            if(!cutFile.exists()){
                cutFile.mkdirs();
            }

            File outputFile = new File(cutFile.getAbsolutePath() ,"cut_"+System.currentTimeMillis()+".wav");
            outputFile.createNewFile();

            a.WriteFile(outputFile, start, end, new CallBack<String>() {
                @Override
                public void success(String bytes) {
                    LogUtil.LOG_V("[WriteFile]->success = "+bytes);
                    if (callBack!=null){
                        callBack.success(bytes);
                    }
                }

                @Override
                public void error(String s) {
                    LogUtil.LOG_V("[WriteFile]->error = "+s);
                    if (callBack!=null){
                        callBack.error(s);
                    }
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
